package com.Sun.Helper;

import java.util.ArrayList;
import java.util.List;

import com.Sun.Model.encryptedIndex;
import com.Sun.Model.fileScorePair;

public class searchHelper {
	public static double getScore(encryptedIndex enIndex,encryptedIndex trap)
	{
		double[] MD1=enIndex.getMD1();
		double[] MD2=enIndex.getMD2();
		double[] MQ1=trap.getMD1();
		double[] MQ2=trap.getMD2();
		double score=0;
		for(int i=0;i<MD1.length;i++)
		{
			double tempScore=MD1[i]*MQ1[i]+MD2[i]*MQ2[i];
			score+=tempScore;
		}
		return score;
	}
	public static void update(List<fileScorePair>fspList,fileScorePair filsp,int mostK)
	{
		if(fspList.size()<mostK)
		{
			fspList.add(filsp);
		}
		else
		{
			double min=getMinScore(fspList);
			if(filsp.getScore()>min)
			{
				for(int i=0;i<fspList.size();i++)
				{
					if(fspList.get(i).getScore()==min)
					{
						fspList.set(i, filsp);
						break;
					}
				}
			}
		}
	}
	public static double getMinScore(List<fileScorePair>fspList)
	{
		double min=0;
		if(fspList.size()>0)
		{
			min=fspList.get(0).getScore();
			for(int i=1;i<fspList.size();i++)
			{
				if(fspList.get(i).getScore()<min)
				{
					min=fspList.get(i).getScore();
				}
			}
			
		}
		return min;
	}
}
